package kayao.client.input;

import kayao.client.data.SharedMemory;
import kayao.client.data.Slave;
import kayao.client.network.message.LogOutMessage;
import kayao.client.network.message.MessageHandler;
import kayao.client.scene.objects.CarNode;
import kayao.common.Client;
import kayao.common.KayaoValues;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;

public class KeyInput implements OnKeyListener {
	private boolean rightDown = false;
	private boolean leftDown = false;

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		if(event.getAction() == KeyEvent.ACTION_DOWN) {
			switch(keyCode) {
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				rightDown();
				return true;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				leftDown();
				return true;
			case KeyEvent.KEYCODE_BACK:
//				System.out.println("*** Number of slave.send: "+Slave.slaveSendCounter);
//				System.out.println("*** Number of slave.sendToResponse: "+Slave.slaveSendToResponseCounter);
				
				String[] kings = Slave.getMyKings();
				Client[] teamclients = SharedMemory.getInstance().getTeamMates();
				MessageHandler.setLogoutCountInNetwriteThread((1+kings.length+teamclients.length));
				MessageHandler.insertOutgoingMessage(new LogOutMessage(KayaoValues.SERVER_IP));
				for (int i=0; i<kings.length; i++){
//					System.out.println("kings[i]: "+kings[i]+" own ip: "+SharedMemory.getInstance().getMyself().getIP());
					try {
						if (kings[i].equals(null));
					} catch (NullPointerException e){
						MessageHandler.decreaseLogoutCounter();
						continue;
					}
					if (kings[i].equals(SharedMemory.getInstance().getMyself().getIP())){
						MessageHandler.decreaseLogoutCounter();
						continue;
					} else {
						MessageHandler.insertOutgoingMessage(new LogOutMessage(kings[i]));
					}
				}
				for (int i=0; i<teamclients.length; i++){
					try {
						if (teamclients[i].getIP().equals(null));
					} catch (NullPointerException e){
						MessageHandler.decreaseLogoutCounter();
						continue;
					}
					if (teamclients[i].getIP().equals(SharedMemory.getInstance().getMyself().getIP())){
						MessageHandler.decreaseLogoutCounter();
						continue;
					} else {
						MessageHandler.insertOutgoingMessage(new LogOutMessage(teamclients[i].getIP()));
					}
				}
				return false;
			}
		}
		if(event.getAction() == KeyEvent.ACTION_UP) {
			switch(keyCode) {
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				rightUp();
				return true;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				leftUp();
				return true;
			}
		}
		return false;
	}

	private void rightDown() {
		if (!rightDown) {
			CarNode avatar = SharedMemory.getInstance().getAvatar();
			// Temporary for moving around
			synchronized(avatar) {
				avatar.deltaRotation = -40;
			}
			rightDown = true;
		}
	}

	private void rightUp() {
		if (rightDown) {
			CarNode avatar = SharedMemory.getInstance().getAvatar();
			// Temporary for moving around
			synchronized(avatar) {
				avatar.deltaRotation = 0;
			}
			rightDown = false;
		}
	}
	
	private void leftDown() {
		if (!leftDown) {
			CarNode avatar = SharedMemory.getInstance().getAvatar();
			// Temporary for moving around
			synchronized(avatar) {
				avatar.deltaRotation = 40;
			}
			leftDown = true;
		}
	}

	private void leftUp() {
		if (leftDown) {
			CarNode avatar = SharedMemory.getInstance().getAvatar();
			// Temporary for moving around
			synchronized(avatar) {
				avatar.deltaRotation = 0;
			}
			leftDown = false;
		}
	}	
}
